package com.ld.util;

import org.apache.shiro.crypto.hash.Md5Hash;

import java.util.Random;

public class SaltUtil {

    /**
     * @param length 需要的随机字符串长度(本次采用16)
     */
    public static String getSalt(int length) {
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     *
     * @param passWord 密码
     * @param salt 盐
     * @return 明文盐+密码转换为16位哈希散列
     */
    public static String getHashHex(String passWord,String salt){
        Md5Hash md5Hash=
                new Md5Hash(passWord,salt,1024);
        return md5Hash.toHex();
    }
}
